﻿
import YouHP.Graphics.*;

class Bar extends ChartBase
{
	//Lable数据
	var mLableLeft : Number = 5;		//Lable的左边界	
	var mLableWidth : Number = 150;		//宽度
	
	//图表
	var mChartLeft : Number = mLableLeft + mLableWidth ;
	var mChartWidth : Number = 400;
	var mChartTop : Number = 10;
	var mYGap : Number = 30;
	var mXCount : Number = 5;
	var mXGap : Number = mChartWidth/(mXCount -1);
	var mYCount : Number = 0;
	
	//图表数据
	var lables:Array = new Array();
	var valueLinks:Array = new Array();
	var values:Array = new Array();
	var maxVal:Number = 0;
	var mDefaultColor:Number = 0xBBDDFF;
	var mFontColor:Number = 0x0080EFF;
	
	var txtXGap:Number = 8;
	var txtYGap:Number = 10;
	var mDataHeight : Number = 8;
	
	function Bar(lv : Object)
	{
		mClip = _root.createEmptyMovieClip("Bar", _root.getNextHighestDepth());
        mData = lv;		
        
		//提取图表数据
        ParseData();
		
		//设置图例
		DrawGrid();
		
		//画图
        DrawChart();
	}
	
	private function ParseData()
	{
		var varArr = mData["Var"].split(";");
		var dataCnt : Number = varArr[1];
		var dataArr : Array ;
		
		for(var i=0; i<dataCnt; i++)
		{
			dataArr = mData["Data_" + i].split(";");
			lables.push([dataArr[0]]);
			valueLinks.push([dataArr[1]])
			values.push([dataArr[2]]);			
		}
		
		maxVal = values[0][0];
		mYCount = lables.length;
	}
	
	//以特定风格显示文本
	public function DrawText(textName:String, x:Number, y:Number, textColor:Number, size:Number, align:String)
	{
		var mc : MovieClip = mClip;
		
		var textformat = new TextFormat();
		textformat.color = Color(textColor);
		textformat.align = align;
		textformat.size  = 10;
		
		var label:TextField = mc.createTextField(textName, mc.getNextHighestDepth(), x, y, 0, 0);		
		label.setTextFormat(textformat);
		label.html = true;
		label.autoSize = align;
		label.htmlText = textName;
		label.textColor = textColor;
	}			
	      
    public function DrawGrid()
    {
      	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);
        var pen = new Pen(1.0, mDefaultColor, 100);
		
		var curX = mLableLeft;
		var curY = mChartTop;
		
		//画所有的lables
		for(var i=0; i<lables.length; i++)
		{

			DrawText(lables[i][0], curX, curY+txtYGap, mFontColor, 10, "left") ;			 
			curY += mYGap;		
		}
		
		//画右边的纵向线
		curX = mChartLeft;
		for(i=0; i<mXCount; i++)
		{
			gs.DrawLine(pen, curX, mChartTop, curX, mChartTop + (mYCount)*mYGap);
			curX += mXGap;
		}
    }
	
    public function DrawChart()
    {
      	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);

        var pen = new Pen(1.0, mDefaultColor,100);
		var brush = new SolidBrush(mDefaultColor, 100);
		var curY = mChartTop + 1.5*txtYGap;
		var startX = mChartLeft;
		var curWidth = 0;
		var tmpY = 0;
		var tmpStr = "";
		
		for(var i=0; i<mYCount; i++)
		{
			//画矩形
			curWidth = values[i][0]*mChartWidth/maxVal ;
			gs.DrawRect(pen, startX, curY, curWidth, mDataHeight);
			gs.FillRect(brush, startX, curY, curWidth, mDataHeight);
			
			//显示数量
			if(valueLinks[i][0].length > 0)
			{	
				tmpStr = "<a href='" + valueLinks[i][0] + "'>" + values[i][0] + "</a>";		
			}
			else
			{
				tmpStr = values[i][0];
			}			
			
			DrawText(tmpStr, startX + curWidth + txtXGap, mChartTop + txtYGap + i*mYGap, mFontColor, 10, "left") ;			
			
			curY += mYGap;
		}		
    }		
}